<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body></body>
  <!-- <script src="./lib/Promise-练习重写then方法.js"></script> -->
  <!-- <script src="./lib/Promise.js"></script> -->
  <script src="./lib/Promise_Class.js"></script>
  <!-- <script src="./promise.js"></script> -->
  <script>
    // let p = new Promise((resolve, reject) => {
    //   // setTimeout(() => {
    //   resolve(2) // 1: value
    //   // }, 2000)
    //   // reject(2) // 2: reason
    // }).then(
    //   value => {
    //     return (value *= 2)
    //   },
    //   reason => {
    //     console.log('onRejected1', reason)
    //   }
    // ).then(value => {
    //   console.log(value)
    // })
  </script>

  <script>
    function fn1() {
      let p = new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve(2)
        }, 1000)
      })
        .then(
          value => {
            console.log('then1 -res- ', value)
            return (value *= 2)
          },
          reason => {
            console.log('then1 -reason- ', reason)
          }
        )
        .then(
          value => {
            console.log('then2 -res- ', value)
            throw (value *= 2)
          },
          reason => {
            console.log('then2 -reason- ', reason)
          }
        )
        .then(
          value => {
            console.log('then3 -value- ', value)
          },
          reason => {
            console.log('then3 -reason- ', reason)
            return new Promise((resolve, reject) => {
              reject((reason *= 2))
            })
          }
        )
        .then()
        .catch(reason => {
          console.log('catch -reason- ', reason)
          return (reason *= 2)
        })
        .then(res => {
          console.log('终极 then res => ', res)
        })
    }
    // fn1()


    function fn2() {
      // 如果是一般值 p1成功 value就是这个值
      const p1 = Promise.resolve(2)
      // 如果是成功的 p2成功 value就是这个promise的值
      const p2 = Promise.resolve(Promise.resolve(3))
      // 如果是失败的promise p3失败 reason是这个promise的reason
      const p3 = Promise.resolve(Promise.reject(4))
      p1.then(value => {
        console.log('p1', value)
      })
      p2.then(value => {
        console.log('p2', value)
      })
      p3.catch(reason => {
        console.log('p3', reason)
      })
    }
    // fn2()

    function fn3() {
      const p1 = Promise.resolve(1)
      const p2 = new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve(2)
        }, 3000)
      })
      // 失败的promise
      const p3 = Promise.resolve(Promise.reject(3))
      const p4 = Promise.resolve(Promise.resolve(4))
      // 失败的promise
      const p5 = Promise.resolve(Promise.reject(5))
      // 可以传入一个非promise 非promise是成功态
      Promise.all([p1, p2, 20]).then(values => {
        console.log(values)
      }).catch(reason => {
        // 只有有一次是失败 那么返回的结果就是失败的 而且是第一个失败的 因为内部返回的promise只要reject一执行 状态就不可以就更该了 所以reject只会修改一次状态
        console.log(reason)
      })
    }
    fn3()

    function fn4() {
      const p1 = new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve('p1')
        }, 3000)
      })
      const p2 = new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve('p2')
        }, 1000)
      })
      const p3 = new Promise((resolve, reject) => {
        setTimeout(() => {
          reject('p3')
        }, 2000)
      })
      Promise.race([p1, p2, p3]).then(value => {
        console.log('resolved', value)
      }).catch(reason => {
        console.log('catch', reason)
      })
    }
    // fn4()

    function fn5() {
      const p1 = Promise.resolvedDelay(10, 2000)
      const p2 = Promise.rejectedDelay(20, 3000)
      p1.then(value => {
        console.log('p1', value)
      })
      p2.catch(reason => {
        console.log('p2', reason)
      })
    }
    // fn5()
  </script>
</html>
